#!/bin/bash
role=$1
weight=$2
vip=$3
routerid=$4
interface=`ifconfig | awk -F" " '{print $1}' | grep -v lo | grep -v ^\s*$`
keep_dir=/export/servers/keepalived-1.2.13
conf_dir=$keep_dir/conf
pid_dir=$keep_dir/pid
sh_dir=/export/sh
WHO=`whoami`
function help()
{
echo -e "Install Tips:#Such as master安装 sh $0 MASTER 100 vip routerid # BACKUP安装 sh $0 BACKUP [LESS THEN 100] vip routerid"
}
if [ $# -eq 0 ]
then
help
exit 1
fi
echo -e "\033[;37;32mCheckNetwork\033[0m"
if ! ping -c 1 -w 1 172.22.197.62 > /dev/null; then
echo -e "\033[;37;31mPlease Check Network Before Setup\033[0m";
exit 1;
else
echo -e "\033[;37;32mOK\033[0m";
fi
echo -e "\033[;37;32mStarting Yum\033[0m"
yum -y install popt-devel openssl openssl-devel make openssl-devel popt popt-devel libnl-devel kernel-devel
echo -e "\033[;37;32mYum Install Done\033[0m"
cd /usr/local/src/keepalived
if [ ! -e keepalived-1.2.13.tar.gz ]
then
wget http://172.22.197.62/CentOS/app/keepalived-1.2.13.tar.gz
tar xzf keepalived-1.2.13.tar.gz
else
tar xzf keepalived-1.2.13.tar.gz
fi
rpm -ivh popt-static-1.13-7.el6.x86_64.rpm
cd keepalived-1.2.13
./configure --prefix=/export/servers/keepalived-1.2.13 --sysconfdir=/export/servers/keepalived-1.2.13/etc/ --bindir=/export/servers/keepalived-1.2.13/bin/ --with-kernel-dir=/usr/src/linux/
make && make install
if [ $? -eq 0 ]
then
echo "make success"
else
echo "make error"
exit 1
fi
cp /export/servers/keepalived-1.2.13/sbin/keepalived /usr/sbin/
cp /export/servers/keepalived-1.2.13/etc/sysconfig/keepalived /etc/sysconfig/
cp ../keepalived /etc/init.d/
chmod +x /etc/init.d/keepalived
chkconfig --add keepalived
chkconfig keepalived on
echo -e "\033[;37;32m创建keepalive配置文件路径\033[0m"
mkdir -p $conf_dir
cat > $conf_dir/keepalived.conf <<EOF
! Configuration File for keepalived
global_defs {
notification_email {
lihuiyw@jd.com
}
notification_email_from lihuiyw@jd.com
smtp_server mail.jd.com
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script check_alive
{
script "/export/sh/check_nginx_alive.sh"
interval 2
weight -10
fail 2
rise 1
}
vrrp_instance VIP_${routerid}
{
state $role
interface $interface
virtual_router_id $routerid
priority $weight
advert_int 2
garp_master_delay 10
smtp_alert
authentication
{
auth_type PASS
auth_pass 123456
}
virtual_ipaddress
{
${vip}/24
}
track_interface
{
$interface
}
track_script
{
check_alive
}
}
EOF
echo -e "\033[;37;32m创建脚本路径\033[0m"
mkdir -p $sh_dir $pid_dir
cat > $sh_dir/check_nginx_alive.sh << EOF
#!/bin/bash
if [ \`pgrep "^nginx" | wc -l\` -eq 0 ]
then
service nginx start
sleep 1
if [ \`pgrep "^nginx" | wc -l\` -eq 0 ]
then
service keepalived stop
fi
fi
EOF
echo -e "\033[;37;32m赋可执行权限\033[0m"
chown -R admin.admin $conf_dir $keep_dir $sh_dir
/export/servers/keepalived-1.2.13/sbin/keepalived --use-file=/export/servers/keepalived-1.2.13/conf/keepalived.conf
>/home/install_keepalived.log
if [ $? -eq 0 ]
then
echo "keepalived install done"
fi